package com.wntms.auser.service.impl;

import com.wntms.auser.dao.UserDao;
import com.wntms.auser.pojo.LoginUser;
import com.wntms.auser.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findByUserName(username);


        if(user == null) {
            throw new RuntimeException("用户名不存在");
        }

        user.setPassword("{noop}" + user.getPassword());
        UserDetails loginUser = new LoginUser(user);
        //将userDetails存储到缓存中
        return loginUser;
    }
}
